package net.muchine.test.ddd.repository.hibernate;

import net.muchine.test.ddd.domain.Product;
import net.muchine.test.ddd.repository.ProductRepository;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class HibernateProductRepository implements ProductRepository {
	@Autowired SessionFactory factory;
	
	
	public void save(Product product) {
		this.factory.getCurrentSession().save(product);
	}

	@SuppressWarnings("unchecked")
	public Product find(String productName) {
		Query query = this.factory.getCurrentSession().createQuery("from Product where name=:name")
						.setString("name", productName);
		Product product = (Product) query.uniqueResult();
		
		return product;
	}
}
